package com.xjt.wx_admin.util;

import com.xjt.wx_admin.entity.User;
import com.xjt.wx_admin.security.LoginUser;
import lombok.experimental.UtilityClass;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

@UtilityClass
public class SecurityUtils {



    public Authentication getAuthentication()	 {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 获取用户
     */
    public User getUser() {
        Authentication authentication = getAuthentication();
        User user = getUser(authentication).getUser();
        user.setPassword(null);
        return user;
    }


    public LoginUser getUser(Authentication authentication) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof LoginUser) {
            return (LoginUser) principal;
        }
        return null;
    }


    public Long getUserId(){
        return getUser().getId();
    }
}
